package com.webdev.base.controller;

import com.webdev.base.pojo.Admin;
import com.webdev.base.service.IAdminService;
import com.webdev.base.util.TokenUtils;
import com.webdev.base.vo.AdminVo;
import com.webdev.base.vo.RespBean;
import com.webdev.base.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

@Controller
@Slf4j
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @ResponseBody
    @PostMapping("/login")
    public RespBean login(String account, String password) {
        AdminVo admin = adminService.login(account, password);
        if (Objects.nonNull(admin)) {
            admin.setToken(TokenUtils.getToken(admin.getAdminsId()));
            return RespBean.success(admin);
        } else return RespBean.error(RespBeanEnum.LOGIN_ERROR);
    }
}
